# coding=utf-8
from django.db import models
from com.kjssoft.common.globals import DBTableAssist
from com.kjssoft.eagleye.models import EEUser

__author__ = 'KJS'

class Application(models.Model):
    """
    绑定到社交平台的应用
    """
    name = models.CharField(max_length=255, unique=True)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('eagleye_social_application')

class Platform(models.Model):
    """
    社交平台的信息
    """
    name = models.CharField(max_length=255, unique=True)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('eagleye_social_platform')

class Bind(models.Model):
    """
    应用与社交平台绑定的信息
    """
    application = models.ForeignKey(Application, related_name='application_platform_bind_application')
    platform    = models.ForeignKey(Platform, related_name='application_platform_bind_platform')
    bind_ms     = models.IntegerField()
    app_id      = models.CharField(max_length=255, null=True, blank=True)
    app_key     = models.CharField(max_length=255, null=True, blank=True)
    app_secret  = models.CharField(max_length=255, null=True, blank=True)
    ap_face     = models.URLField(null=True, blank=True)

    def __unicode__(self):
        return '%s bind in %s.' % (self.application, self.platform)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('eagleye_social_bind')
        unique_together = (
            ('application', 'platform'),
        )

class SocialUser(models.Model):
    social_uuid = models.IntegerField()
    name        = models.CharField(max_length=500)
    nickname    = models.CharField(max_length=500)
    snapshot    = models.URLField(null=True, blank=True)
    platform    = models.ForeignKey(Platform, related_name='ap_bind_user_platform')
    bind_user   = models.OneToOneField(EEUser, related_name='ap_bind_user_bind_user', null=True, blank=True)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('eagleye_social_user')
        unique_together = (
            ('social_uuid', 'platform'),
        )

class Auth(models.Model):
    social_user   = models.ForeignKey(SocialUser, related_name='auth_social_user')
    bind          = models.ForeignKey(Bind, related_name='auth_bind')
    auth_ms       = models.IntegerField()
    refresh_token = models.CharField(max_length=500, null=True, blank=True)
    access_token  = models.CharField(max_length=500, null=True, blank=True)
    expire_in     = models.IntegerField(null=True, blank=True)

    class Meta:
        db_table = DBTableAssist.get_db_table_name('eagleye_social_user_auth')
        unique_together = (
            ('social_user', 'bind'),
        )